package top.smallway.bs_vr.recycelviewAdapeter;

import android.content.Context;
import android.util.AttributeSet;
import android.view.View;

import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

public class GridLayoutManagerCenter extends GridLayoutManager {

    public GridLayoutManagerCenter(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
    }

    public GridLayoutManagerCenter(Context context, int spanCount) {
        super(context, spanCount);
    }

    public GridLayoutManagerCenter(Context context, int spanCount, int orientation, boolean reverseLayout) {
        super(context, spanCount, orientation, reverseLayout);
    }

    @Override
    public void onLayoutChildren(RecyclerView.Recycler recycler, RecyclerView.State state) {
        super.onLayoutChildren(recycler, state);
        centerItems();
    }

    private void centerItems() {
        final int childCount = getChildCount();
        if (childCount == 0) {
            return;
        }

        final int leftEdge = getPaddingLeft();
        final int rightEdge = getWidth() - getPaddingRight();
        final int topEdge = getPaddingTop();
        final int bottomEdge = getHeight() - getPaddingBottom();

        for (int i = 0; i < childCount; i++) {
            final View view = getChildAt(i);

            final int width = view.getWidth();
            final int height = view.getHeight();

            final int left = getDecoratedLeft(view);
            final int right = getDecoratedRight(view);
            final int top = getDecoratedTop(view);
            final int bottom = getDecoratedBottom(view);

            if (left >= leftEdge && right <= rightEdge && top >= topEdge && bottom <= bottomEdge) {
                continue;
            }

            final int x = left + width / 2;
            final int y = top + height / 2;

            if (x < leftEdge + getWidth() / 2) {
                offsetChildrenHorizontal(leftEdge + getWidth() / 2 - x);
            } else if (x > rightEdge - getWidth() / 2) {
                offsetChildrenHorizontal(rightEdge - getWidth() / 2 - x);
            }

            if (y < topEdge + getHeight() / 2) {
                offsetChildrenVertical(topEdge + getHeight() / 2 - y);
            } else if (y > bottomEdge - getHeight() / 2) {
                offsetChildrenVertical(bottomEdge - getHeight() / 2 - y);
            }
        }
    }

    @Override
    public void setSpanSizeLookup(SpanSizeLookup spanSizeLookup) {
        super.setSpanSizeLookup(new CenterSpanSizeLookup(spanSizeLookup, getSpanCount()));
    }

    private static class CenterSpanSizeLookup extends SpanSizeLookup {

        private SpanSizeLookup mSpanSizeLookup;
        private int mSpanCount;

        public CenterSpanSizeLookup(SpanSizeLookup spanSizeLookup, int spanCount) {
            mSpanSizeLookup = spanSizeLookup;
            mSpanCount = spanCount;
        }

        @Override
        public int getSpanSize(int position) {
            if(position == 0) { // 第一个item要居中
                return mSpanCount;
            }
            return mSpanSizeLookup.getSpanSize(position);
        }
    }
}

